package com.example.leetcode.queue;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {

    private final Queue<Integer> queueOne = new LinkedList<>();
    private final Queue<Integer> queueTwo = new LinkedList<>();

    private Integer queueTailNum = null;

    public MyStack() {

    }

    public void push(int x) {
        Queue<Integer> curQueue;
        if(queueOne.isEmpty() && queueTwo.isEmpty()){
            curQueue = queueOne;
        } else if(!queueOne.isEmpty()){
            curQueue = queueOne;
        } else {
            curQueue = queueTwo;
        }
        curQueue.add(x);
        queueTailNum = x;
    }

    public int pop() {
        if(queueOne.isEmpty() && queueTwo.isEmpty()){
            return -1;
        } else if(!queueOne.isEmpty()){
            if(queueOne.size() == 1){
                queueTailNum = null;
                return queueOne.poll();
            }
            while(queueOne.size() > 1){
                if(queueOne.size() == 2){
                    queueTailNum = queueOne.peek();
                }
                queueTwo.add(queueOne.poll());
            }
            return queueOne.poll();
        } else {
            if(queueTwo.size() == 1){
                queueTailNum = null;
                return queueTwo.poll();
            }
            while (queueTwo.size() > 1){
                if(queueTwo.size() == 2){
                    queueTailNum = queueTwo.peek();
                }
                queueOne.add(queueTwo.poll());
            }
            return queueTwo.poll();
        }
    }

    public int top() {
        return queueTailNum;
    }

    public boolean empty() {
        return queueOne.isEmpty() && queueTwo.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

public class leetcode255 {
    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.empty());
    }
}
